/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.core.action.save;

import com.inspur.edp.bef.api.action.VoidActionResult;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bff.api.manager.assembler.IAssemblerManager;
import com.inspur.edp.bff.api.manager.context.SaveContext;
import com.inspur.edp.bff.api.manager.context.TimeStampContext;
import com.inspur.edp.bff.spi.AbstractFSAction;

public class SaveAction extends AbstractFSAction<VoidActionResult> {


    public SaveAction() {
    }

    @Override
    protected void execute() {
        IAssemblerManager assemblerManager = getContext().getAssemblerManager();
        if (assemblerManager == null) {
            executeSave();
            return;
        }

        SaveContext saveContext = new SaveContext();

        //① BeforeSave
        assemblerManager.getSaveAssembler().doBeforeSave(getContext(), saveContext);
        //②
        assemblerManager.getSaveAssembler().doSave(getContext(), saveContext);
        //3 AfterSave
        assemblerManager.getSaveAssembler().doAfterSave(getContext(), saveContext);
    }

    private void executeSave() {
        //获取外部服务传过来的上下文信息
        TimeStampContext context = getContext().getBffManager().getTimeStampContext();

        for (IStandardLcp lcp : getContext().getLcps().values()) {
            lcp.getBefContext().setEnableTimeStampLastModifiedBy(context.isEnableTimeStampLastModifiedBy());
            lcp.save();
        }
    }
}
